
********************************************************************************
//Reported Export Data from COMTRADE

loc run = 0
if `run' == 1{
    
use "$data\ALL_FILES_h1_export.dta", clear

drop if reporteriso3=="WLD"
drop if partneriso3=="WLD"
drop if reporteriso3=="EUN"
drop if partneriso3=="EUN"

rename productcode h6
rename year y
rename partneriso3 c // Importer 

replace tradevalue = tradevalue*1000 // Converting from thousands of USD to USD

collapse (sum) tradevalue, by(c h6 y) fast

rename tradevalue exp_reported
rename h6 hs

* Merging below the file with consolidated HS codes (variable h) and their mapping to ANY hs6 codes from any revision ----- the ultimate HS code that will be used is the variable h
merge m:1 hs using "$data/consolidation_1996_2012_data_convert2017.dta"
* HS6 codes that are not included in the consolidation dataset have not changed across revisions and therefore they remain themselves
replace h=hs if _m==1
drop if _m==2
drop _m
* Since consolidated HS codes are mapped to families of HS6 the data needs to be collapsed to the level of the HS consolidated code
collapse (sum) exp_reported, by(c h y) fast
rename h h6
sort c h6 y
save  "$data\partner_import_h1.dta", replace

}




********************************************************************************
//Inequality

loc run =0
if `run' == 1{
*net install github, from("https://haghish.github.io/github/")
*cap github uninstall pip
*github install worldbank/pip, replace
*Source: https://worldbank.github.io/pip/

pip, clear
keep if gini!=.
rename country_code iso3code
*Keeping one observation per country-year
bys iso3code year: gen x=_n
count if x==1 /* n=2,176 */
duplicates tag iso3code year, g(dup)
tab dup /* n=349 in which dup>0 15% */
*2 sources of variation: welfare type (Consumption==1, Income==2) and reporting level (rural, urban, national)
gen cat=.
replace cat=1 if reporting_level=="rural" & welfare_type==1
replace cat=2 if reporting_level=="rural" & welfare_type==2
replace cat=3 if reporting_level=="urban" & welfare_type==1
replace cat=4 if reporting_level=="urban" & welfare_type==2
replace cat=5 if reporting_level=="national" & welfare_type==1
replace cat=6 if reporting_level=="national" & welfare_type==2
*One observation per country-year (mode)
bys iso3code: egen mode_cat=mode(cat), maxmode
gen id=1 if cat==mode_cat
bys iso3code year: egen max=max(id)
drop if dup>0 & cat!=mode_cat & max==1
drop if dup>0 & max==. & reporting_level!="national" /*CHN before 1990 data*/
drop x dup
bys iso3code year: gen x=_n
count if x==1 /* n=2,176 */
duplicates tag iso3code year, g(dup)
tab dup /* n=2,176 in which dup>0 */
egen country=group(iso3code)
xtset country year
sort country year
tsfill
carryforward iso3code, replace
sort iso3code year
by iso3code: ipolate gini year, gen(gini_i)
label var gini_i "Gini coefficient (interpolated)"
keep iso3code year gini gini_i 
rename iso3code c
rename year y
save "$data/gini.dta", replace
}

********************************************************************************
//WDI Commodity Shares and GDP
loc run =0
if `run' == 1{
//Called on June 14, 2024; WDI subject to future revision
wbopendata, indicator(TM.TAX.MRCH.WM.AR.ZS; TX.VAL.FOOD.ZS.UN; TX.VAL.FUEL.ZS.UN; TX.VAL.MMTL.ZS.UN; NY.GDP.PCAP.PP.CD; NY.GDP.PCAP.CD; NE.IMP.GNFS.ZS; NE.IMP.GNFS.KN; SP.POP.TOTL) long clear

rename tx_val_mmtl_zs_un ores_metal_exports
rename tx_val_fuel_zs_un fuel_exports
rename tx_val_food_zs_un food_exports
rename tm_tax_mrch_wm_ar_zs tariff_wdi
rename ne_imp_gnfs_zs imports_gdp
rename ne_imp_gnfs_kn imports
rename sp_pop_totl pop

egen commodity_exports = rowtotal(ores_metal_exports fuel_exports food_exports) if ores_metal_exports!=. |  fuel_exports!=. | food_exports!=.

foreach var of varlist ores_metal_exports fuel_exports food_exports {
                replace `var'=0 if `var'==. & commodity_exports!=.
        }


rename countrycode c
rename year y

gen loggdppc = ln(ny_gdp_pcap_pp_cd) /* GDP per capita, PPP (current international $) */
gen loggdppc2 = ln(ny_gdp_pcap_cd) /* GDP per capita (current US$) */
replace imports = ln(imports)
replace pop = ln(pop)
keep c y *_exports countryname loggdppc loggdppc2 tariff_wdi imports imports_gdp pop

save "$data\wdi_v2.dta", replace
}
